//
//  BaseViewController.m
//  SinaNews
//
//  Created by qianfeng on 15-6-14.
//  Copyright (c) 2015年 耿宁. All rights reserved.
//

#import "BaseViewController.h"
#import "NewsCell.h"
#import "ImageCell.h"
#import "NewsModel.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
    [self creatHttpRequest];
    
    
}

#pragma mark-创建下载请求
- (void)creatHttpRequest {
    _manager=[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
}

#pragma mark-下载数据
-(void)downloadData:(NSString *)url {
    [self.dataArr removeAllObjects];
    __weak typeof (self)weakSelf=self;
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            //NSLog(@"##%@",dict);
            NSDictionary *data=dict[@"data"];
            
            NSArray *list1=data[@"list"];
            
             for (NSDictionary *itemDict in list1) {
                 NewsModel *model=[[NewsModel alloc]init];
                 [model setValuesForKeysWithDictionary:itemDict];
                 if (model.pics) {
                    NSDictionary *pics=model.pics;
                     model.list=pics[@"list"];
            
                 }
                 
                [model setValuesForKeysWithDictionary:itemDict];
                
                [weakSelf.dataArr addObject:model];
            }
            // NSLog(@"%@",weakSelf.dataArr);
            [weakSelf.tableView reloadData];
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"数据下载失败");
    }];

}



//创建表格视图
- (void)creatTableView {
    self.dataArr = [[NSMutableArray alloc] init];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64-49) style:UITableViewStylePlain];
    self.tableView.backgroundColor=[UIColor lightGrayColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%ld",self.dataArr.count);
    return self.dataArr.count-5;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /*
    NewsCell *cell=[tableView dequeueReusableCellWithIdentifier:@"NewsCell" forIndexPath:indexPath];
    ImageCell *cell2=[tableView dequeueReusableCellWithIdentifier:@"ImageCell" forIndexPath:indexPath];
    
    //NSLog(@"%@",self.dataArr[indexPath.row]);
    NewsModel *model=self.dataArr[indexPath.row];
    if (model.pics.count==0) {
        [cell showDataWithModel:model];
        return cell;
    }else {
    [cell2 showDataWithModel:model];
    }
     return cell2;
     */
    return nil;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
